package com.zhouyu.nettyclient.handler;

import com.zhouyu.proto.PeopleProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.log4j.Log4j2;

/**
 * @Description:客户端处理器
 */
@Log4j2
public class ProtoClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        try {
            log.info("<<<客户端收到消息,开始处理:{}",msg);
        } finally {

        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("read Complete");
        ctx.fireChannelReadComplete();
    }
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(">>>连接上服务端，开始发消息了....");
        PeopleProto.People.Builder builder = PeopleProto.People.newBuilder();
        builder.setAge(18);
        builder.setName("zhouyu");
        ctx.writeAndFlush(builder.build());
    }
}
